package Dao;
import java.sql.*;
import java.sql.Connection;
public class DatabaseConn {
	private static Connection conn;
	 private static PreparedStatement ps;
	 
	 public static String URL = "jdbc:mysql://localhost:3306/POS_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
	 public static String USER = "root";
	 public static String PASS = "123456";
	 
	 private static String Database = "use POS_db";
	 
	 private DatabaseConn() {
	 
	 }
	 
	 /*public static Connection getCon() {
		 try {
		     if (conn == null || conn.isClosed()) {
		       Class.forName("com.mysql.cj.jdbc.Driver");
		       conn = DriverManager.getConnection(url,USER,PASS);
		     }
		   } catch (ClassNotFoundException e) {
		     e.printStackTrace();
		   } catch (SQLException e) {
		     e.printStackTrace();
		   }
		   return conn;
	 }*/
	 
	 public static Connection getConn() {
		 try {
		     if (conn == null || conn.isClosed()) {
		       Class.forName("com.mysql.cj.jdbc.Driver");
		       conn = DriverManager.getConnection(URL,USER,PASS);
		       ps = conn.prepareStatement(Database);
			   ps.execute();
		     }
		   } catch (ClassNotFoundException e) {
		     e.printStackTrace();
		   } catch (SQLException e) {
		     e.printStackTrace();
		   }
		   return conn;
		   
	 }
	 
	 public static void close(Connection conn,PreparedStatement ps,ResultSet rs) {
	   try {
	     if (rs != null) {
	       rs.close();
	     }
	     if (ps != null) {
	       ps.close();
	     }
	     if (conn != null) {
	       conn.close();
	       //System.out.println("中断与POS系统的连接");
	     }
	   } catch (SQLException e) {
	     e.printStackTrace();
	   }
	 }
}
